﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Watson.Core.Tools;
using ITOMMS.Entity;
using ITOMMS.Business;

namespace ITOMMS.Web.InsideManage
{
    public partial class BackLogAdd : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownListHelper.InitDropList(ddlState, typeof(ITOMMS.Business.PubEnums.TroubleState));
                if (Request.Params["id"] != null)
                {
                    string sid = Request.Params["id"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        DataShow(pid);
                    }
                }
            }
        }

            private void DataShow(int troubleId)
            {
                tbWorkPlanEntity entity = new tbWorkPlanEntity();
                entity = entity.GetById(troubleId);

                if (entity != null)
                {
                    ViewState["Entity"] = entity;

                    txtTitle.Text = entity.Title;
                    txtContent.Text = entity.Content;
                    ddlState.SelectedValue = entity.State.ToString();
                    if (entity.DoDate.HasValue)
                    {
                        txtDoDate.Text = entity.DoDate.Value.ToShortDateString();                    
                    }
                }
            }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Entity.tbWorkPlanEntity entity = new ITOMMS.Entity.tbWorkPlanEntity();
                if (ViewState["Entity"] != null)
                {
                    entity = ViewState["Entity"] as tbWorkPlanEntity;
                }
                else
                {
                    entity.CreateBy = SessionHelper.UserID;
                    entity.CreateDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }
                entity.Title = txtTitle.Text;
                entity.Content = txtContent.Text;
                entity.State = int.Parse(ddlState.SelectedValue);
                DateTime rs = new DateTime();
                if (DateTime.TryParse(txtDoDate.Text.Trim(), out  rs))
                {
                    entity.DoDate = rs;
                }
                entity.ModifyBy = SessionHelper.UserID;
                entity.ModifyDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));


                entity.SaveOrUpdate(entity);
                ViewState["Entity"] = entity;
                MessageboxInfo.MessageBox(this, MessageType.SUCCEEDED, "保存成功");
            }
            catch (Exception ex)
            {
                log4netHelper.error(ex);
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "保存失败");
            }
            Response.Redirect("BackLog.aspx", true);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("BackLog.aspx", true);
        }

    }
}
